var personMap = new Object();

function initialize() {
    window.top.location = 'nash/';
    return;
    var persons = getAllPersons();
    for ( var i = 0; i < persons.length; i++ ) {
        personMap[persons[i].id] = persons[i];
        if ( persons[i].type == 'nash' ) {
            document.getElementById('nashTd').innerHTML = persons[i].name;
        }
        if ( persons[i].type == 'situation' ) {
            document.getElementById("theSituationTd").innerHTML = persons[i].name;
        }
    }
    var messages = getAllMessages();
    var nash = messages.nashMessages;
    var situation = messages.situationMessages;
    for ( var g = 0; g < nash.length; g++ ) {
        var span = document.createElement("span");
        var from = "Nobody";
        try {
            if ( nash[g].personFrom != null ) {
                from =  personMap[nash[g].personFrom].name;
            }
        } catch(e) {

        }
        span.innerHTML = "<b>[" + nash[g].formattedTime + "] From: " + from + " To: " + personMap[nash[g].personTo].name + "</b><br/> " + nash[g].text;
        document.getElementById("nashMessages").appendChild(span);
        document.getElementById("nashMessages").appendChild(document.createElement("br"));
        document.getElementById("nashMessages").appendChild(document.createElement("br"));
    }
    for ( var g = 0; g < situation.length; g++ ) {
        var span = document.createElement("span");
        var from = "Nobody";
        try {
            if ( situation[g].personFrom != null ) {
                from =  personMap[situation[g].personFrom].name;
            }
        } catch(e) {
            
        }
        span.innerHTML = "<b>[" + situation[g].formattedTime + "] From: " + from + " To: " + personMap[situation[g].personTo].name + "</b><br/> " + situation[g].text;
        document.getElementById("situationMessages").appendChild(span);
        document.getElementById("situationMessages").appendChild(document.createElement("br"));
        document.getElementById("situationMessages").appendChild(document.createElement("br"));
    }
}

function createNewPerson() {
    var person = {
        name: document.getElementById("name").value,
        emailAddress: document.getElementById("email").value,
        password: document.getElementById("password").value
    };

    jQuery.ajax({
        url: 'nash/person',
        data: JSON.stringify(person),
        contentType: 'application/json',
        type: 'POST',
        success: function() {
            alert("Person added");
            window.top.location = 'index.html';
        },
        error: function() {
            alert("Person not added, bitch");
        },
        async: false
    });


}

function getAllPersons() {
    var returnData;
    jQuery.ajax({
        url: 'nash/person',
        async: false,
        success: function(data) {
            returnData = data;
        }
    });
    return returnData;
}

function getAllMessages() {
    var returnData;
    jQuery.ajax({
        url: 'nash/message',
        async: false,
        success: function(data) {
            returnData = data;
        }
    });
    return returnData;
}

function fillOutSelectBox(id) {
    jQuery.ajax({
        url: 'nash/person',
        success: function(data) {
            var select = document.getElementById(id);
            for ( var i = 0; i < data.length; i++ ) {
                var option = document.createElement("option");
                option.value = data[i].id;
                option.innerHTML = data[i].name;
                select.appendChild(option);
            }
        }
    });
}

function submitChange(location, idPrefix) {

    var select = document.getElementById(idPrefix + "Select");

    var postData = {
        to: select[select.selectedIndex].value,
        description: document.getElementById(idPrefix + "Description").value,
        password: document.getElementById("password").value
    }

    jQuery.ajax({
        url: 'nash/' + location,
        async: false,
        success: function(data) {
            alert("Event added");
            window.top.location = 'index.html';
        },
        error: function(data) {
            alert("Event not added, bitch");
        },
        type: "POST",
        data: JSON.stringify(postData),
        contentType: "application/json"
    })
    
}